package com.ce.pms.system.custservice.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.ce.cetag.model.AjaxSelectModel;
import com.ce.pms.base.vo.SearchVO;
import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.system.custservice.service.ICustEventKindService;
import com.ce.pms.system.custservice.service.ICustEventNatureService;
import com.ce.pms.system.custservice.service.IEmergencyDegreeService;
import com.ce.pms.system.custservice.service.ISignReadingRuleService;
import com.ce.pms.system.custservice.vo.CustEventKindVO;
import com.ce.pms.system.custservice.vo.CustEventNatureVO;
import com.ce.pms.system.custservice.vo.EmergencyDegreeVO;
import com.ce.pms.system.custservice.vo.SignReadingRuleVO;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.annotation.Read;
import com.sitechasia.webx.core.support.Page;

/**
 * 阅签规则
 * 
 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
 * @Created 2008-12-17
 */
public class SystemCustEventSignReadingRuleAction extends PmsBaseAction
		implements ISystemEventAction {
	private static final String		PAGE_EDIT	= "systemCustEventSignReadingRuleEditPage";

	private static final String		PAGE_INIT	= "systemCustEventSignReadingRuleListPage";

	private ICustEventKindService	custEventKindService;

	private ICustEventNatureService	custEventNatureService;

	private IEmergencyDegreeService	emergencyDegreeService;

	private ISignReadingRuleService	signReadingRuleService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ce.pms.system.custservice.web.ISystemEventAction#addPage()
	 */
	public String addPage() {
		List<CustEventNatureVO> custEventNature = this.custEventNatureService
				.queryAllCustEventNature();

		List<EmergencyDegreeVO> emergencyDegree = this.emergencyDegreeService
				.getEmergencyDegreeList();

		this.setAttribute("custEventNature", custEventNature);

		this.setAttribute("emergencyDegree", emergencyDegree);

		return PAGE_EDIT;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ce.pms.system.custservice.web.ISystemEventAction#delete(java.lang
	 * .Long)
	 */
	public void delete(@Read(key = "keyword") Long keyword) {
		try {
			if (this.signReadingRuleService.deleteSignReadingRule(keyword)) {
				this.renderText("true");
			} else {
				this.renderText("false");
			}
		} catch (RuntimeException re) {
			re.printStackTrace();
			throw new BusinessException(
					"custservice:system_delete_inused_exception",
					BusinessException.WARNING);
		}
	}

	/**
	 * 编辑或添加
	 * 
	 * @param signReadingRuleVO
	 * @return
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2008-12-19
	 */
	public void edit(@Read SignReadingRuleVO signReadingRuleVO) {
		signReadingRuleVO.setValidity("IS_USE".equals(signReadingRuleVO
				.getValidity()) ? "1" : "0");

		if (BlankUtil.isBlank(signReadingRuleVO.getKeyword())) {
			signReadingRuleVO.setCreator(this.getUserId());
			this.signReadingRuleService.addSignReadingRule(signReadingRuleVO);
		} else {
			signReadingRuleVO.setModifier(this.getUserId());
			this.signReadingRuleService.editSignReadingRule(signReadingRuleVO);
		}
		this.renderText("true");

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ce.pms.system.custservice.web.ISystemEventAction#editPage()
	 */
	public String editPage(@Read(key = "keyword") Long keyword) {
		// 原信息
		SignReadingRuleVO signReadingRuleVO = this.signReadingRuleService
				.findSignReadingRuleVOById(keyword);

		this.setAttribute("signReadingRuleVO", signReadingRuleVO);

		List<CustEventNatureVO> custEventNature = this.custEventNatureService
				.queryAllCustEventNature();

		List<EmergencyDegreeVO> emergencyDegree = this.emergencyDegreeService
				.getEmergencyDegreeList();

		List<CustEventKindVO> custServEventKind = this.custEventKindService.queryCustEventKind(signReadingRuleVO.getCustEventNatureId());
		
		this.setAttribute("custServEventKind", custServEventKind);
		
		this.setAttribute("custEventNature", custEventNature);

		this.setAttribute("emergencyDegree", emergencyDegree);

		return PAGE_EDIT;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ce.pms.system.custservice.web.ISystemEventAction#existsName(java.
	 * lang.String)
	 */
	public void existsName(@Read(key = "name") String name) {
		try {
			this.signReadingRuleService.checkSignReadingRuleByName(name, null);
			this.renderText("true");
		} catch (RuntimeException re) {
			this.renderText(this.getLocaleMessage(re.getMessage()));
		}
	}

	/**
	 * xml回调
	 * 
	 * @param dataList
	 * @return
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2008-11-25
	 */
	private String generateOutputString(List<AjaxSelectModel> dataList) {

		StringBuffer buffer = new StringBuffer();

		if (dataList != null && dataList.size() > 0) {
			buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
			buffer.append("<select-list>");
			for (int i = 0; i < dataList.size(); i++) {
				AjaxSelectModel node = dataList.get(i);
				buffer.append("<select-node>");
				buffer.append("<option-text>" + node.getText()
						+ "</option-text>");
				buffer.append("<option-id>" + node.getId() + "</option-id>");
				buffer.append("</select-node>");
			}
			buffer.append("</select-list>");
		}

		System.out.println("returnValue---->" + buffer.toString());
		return buffer.toString();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ce.pms.system.custservice.web.ISystemEventAction#initMain()
	 */
	public String initMain() {
		this.queryDataList(15, 1);
		return PAGE_INIT;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ce.pms.system.custservice.web.ISystemEventAction#queryDataList()
	 */
	public String queryDataList(
			@Read(key = "ec_crd", defaultValue = "15") int pageSize,
			@Read(key = "ec_p", defaultValue = "1") int pageNo) {
		SearchVO searchVO = new SearchVO();
		searchVO.setPageNo(pageNo);
		searchVO.setPageSize(pageSize);

		Page page = this.signReadingRuleService
				.queryAllSignReadingRuleByPage(searchVO);

		List<SignReadingRuleVO> voList = page.getResult();

		List<SignReadingRuleVO> signReadingRuleVOList = new ArrayList<SignReadingRuleVO>();

		// 处理显示
		for (SignReadingRuleVO voTmp : voList) {
			if ("1".equals(voTmp.getValidity())) {
				voTmp.setValidity(SysrefcodeDO.IS_USE);
			} else {
				voTmp.setValidity(SysrefcodeDO.IS_NOT_USE);
			}

			signReadingRuleVOList.add(voTmp);
		}

		this.setAttribute("queryDataList", signReadingRuleVOList);

		this.setAttribute("totalRows", Long.valueOf(page.getTotalCount())
				.intValue());

		return PAGE_INIT;
	}

	/**
	 * @param custEventKindService the custEventKindService to set
	 */
	public void setCustEventKindService(
			ICustEventKindService custEventKindService) {
		this.custEventKindService = custEventKindService;
	}

	/**
	 * @param custEventNatureService the custEventNatureService to set
	 */
	public void setCustEventNatureService(
			ICustEventNatureService custEventNatureService) {
		this.custEventNatureService = custEventNatureService;
	}

	/**
	 * @param emergencyDegreeService the emergencyDegreeService to set
	 */
	public void setEmergencyDegreeService(
			IEmergencyDegreeService emergencyDegreeService) {
		this.emergencyDegreeService = emergencyDegreeService;
	}

	/**
	 * @param signReadingRuleService the signReadingRuleService to set
	 */
	public void setSignReadingRuleService(
			ISignReadingRuleService signReadingRuleService) {
		this.signReadingRuleService = signReadingRuleService;
	}

	/**
	 * 级联查询
	 * 
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2008-12-23
	 */
	public void unitedSelect(@Read(key = "selectId") Long custEventNatureId) {
		// String custEventNatureId = getRequest().getParameter(
		// "custEventNatureId");

		List<CustEventKindVO> custEventKindVOList = this.custEventKindService
				.queryCustEventKind(custEventNatureId);

		List<AjaxSelectModel> ajaxList = new ArrayList<AjaxSelectModel>();

		// 添加楼栋数据集
		for (CustEventKindVO custEventKindVO : custEventKindVOList) {
			AjaxSelectModel node = new AjaxSelectModel();

			node.setId(custEventKindVO.getKeyword().toString());
			node.setText(custEventKindVO.getName());

			ajaxList.add(node);

		}

		byte[] bytes;
		String formatMsg = "";
		formatMsg = this.generateOutputString(ajaxList);
		this.getResponse().setHeader("Cache-Control", "no-cache");
		this.getResponse().setContentType("text/xml");
		try {
			bytes = formatMsg.getBytes("UTF-8");
			this.getResponse().getOutputStream().write(bytes);
		} catch (IOException e) {

			e.printStackTrace();
		}
	}
}
